<!DOCTYPE html>
<html lang="zh-CN">
    <head>
        <meta charset="utf-8">
        <meta name="viewport" content="width=device-width, initial-scale=1">
        <meta name="robots" content="noodp" />
        <meta http-equiv="X-UA-Compatible" content="IE=edge, chrome=1">
        <title>《Dart语言基础之Map》 - Lucklyの博客 - 在阅读中遇见自己</title><meta name="Description" content="早起的年轻人做的网站"><meta property="og:title" content="《Dart语言基础之Map》" />
<meta property="og:description" content="
本文主要介绍
" />
<meta property="og:type" content="article" />
<meta property="og:url" content="https://luckly.work/dart%E8%AF%AD%E8%A8%80%E5%9F%BA%E7%A1%80%E4%B9%8Bmap/" />
<meta property="og:image" content="https://luckly.work/logo.png"/>
<meta property="article:published_time" content="2021-03-29T19:18:14+08:00" />
<meta property="article:modified_time" content="2021-03-29T19:18:14+08:00" />
<meta name="twitter:card" content="summary_large_image"/>
<meta name="twitter:image" content="https://luckly.work/logo.png"/>

<meta name="twitter:title" content="《Dart语言基础之Map》"/>
<meta name="twitter:description" content="
本文主要介绍
"/>
<meta name="application-name" content="LoveIt">
<meta name="apple-mobile-web-app-title" content="LoveIt"><meta name="theme-color" content="#ffffff"><meta name="msapplication-TileColor" content="#da532c"><link rel="shortcut icon" type="image/x-icon" href="/favicon.ico" />
        <link rel="icon" type="image/png" sizes="32x32" href="/favicon-32x32.png">
        <link rel="icon" type="image/png" sizes="16x16" href="/favicon-16x16.png"><link rel="apple-touch-icon" sizes="180x180" href="/apple-touch-icon.png"><link rel="mask-icon" href="/safari-pinned-tab.svg" color="#5bbad5"><link rel="manifest" href="/site.webmanifest"><link rel="canonical" href="https://luckly.work/dart%E8%AF%AD%E8%A8%80%E5%9F%BA%E7%A1%80%E4%B9%8Bmap/" /><link rel="prev" href="https://luckly.work/dart%E8%AF%AD%E8%A8%80%E5%9F%BA%E7%A1%80%E4%B9%8Bdata/" /><link rel="next" href="https://luckly.work/%E5%9C%A8flutter%E4%B8%AD%E5%88%B6%E4%BD%9C%E5%A4%96%E5%8D%96%E5%BA%94%E7%94%A8/" /><link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/normalize.css@8.0.1/normalize.min.css"><link rel="stylesheet" href="/css/style.min.css"><link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/@fortawesome/fontawesome-free@5.13.0/css/all.min.css"><link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/animate.css@3.7.2/animate.min.css"><script type="application/ld+json">
    {
        "@context": "http://schema.org",
        "@type": "BlogPosting",
        "headline": "《Dart语言基础之Map》",
        "inLanguage": "zh-CN",
        "mainEntityOfPage": {
            "@type": "WebPage",
            "@id": "https:\/\/luckly.work\/dart%E8%AF%AD%E8%A8%80%E5%9F%BA%E7%A1%80%E4%B9%8Bmap\/"
        },"image": [{
                            "@type": "ImageObject",
                            "url": "https:\/\/luckly.work\/images\/Apple-Devices-Preview.png",
                            "width":  3200 ,
                            "height":  2048 
                        }],"genre": "posts","keywords": "flutter","wordcount":  1265 ,
        "url": "https:\/\/luckly.work\/dart%E8%AF%AD%E8%A8%80%E5%9F%BA%E7%A1%80%E4%B9%8Bmap\/","datePublished": "2021-03-29T19:18:14+08:00","dateModified": "2021-03-29T19:18:14+08:00","license": "This work is licensed under a Creative Commons Attribution-NonCommercial 4.0 International License.","publisher": {
            "@type": "Organization",
            "name": "xxxx","logo": {
                    "@type": "ImageObject",
                    "url": "https:\/\/luckly.work\/images\/avatar.png",
                    "width":  528 ,
                    "height":  560 
                }},"author": {
                "@type": "Person",
                "name": "早起的年轻人"
            },"description": ""
    }
    </script></head>
    <body header-desktop="fixed" header-mobile="auto"><script type="text/javascript">(window.localStorage && localStorage.getItem('theme') ? localStorage.getItem('theme') === 'dark' : ('auto' === 'auto' ? window.matchMedia('(prefers-color-scheme: dark)').matches : 'auto' === 'dark')) && document.body.setAttribute('theme', 'dark');</script>

        <div id="mask"></div><div class="wrapper"><div class="logo-wrapper">
  <a href="/%20/" class="logo">Lucklyの博客 - 在阅读中遇见自己</a>
</div>

<nav class="site-navbar">
  <ul id="menu" class="menu">
    <li class="menu-item">
      <a class="menu-item-link" href="/posts/">所有文章</a>
    </li><li class="menu-item">
      <a class="menu-item-link" href="/categories/flutter/">Flutter</a>
    </li><li class="menu-item">
      <a class="menu-item-link" href="/categories/read/">读书笔记</a>
    </li><li class="menu-item">
      <a class="menu-item-link" href="/categories/go/">Go</a>
    </li><li class="menu-item">
      <a class="menu-item-link" href="/tags/">标签</a>
    </li><li class="menu-item">
      <a class="menu-item-link" href="/categories/">分类</a>
    </li><li class="menu-item">
      <a class="menu-item-link" href="/about/">关于</a>
    </li><li class="menu-item">
      <a class="menu-item-link" href="https://github.com/ITmxs">Github</a>
    </li><li class="menu-item">
      <a class="menu-item-link" href="/friend/">友链</a>
    </li>
  </ul>
</nav><main class="main">
                <div class="container"><div class="toc" id="toc-auto">
            <h2 class="toc-title">目录</h2>
            <div class="toc-content" id="toc-content-auto"></div>
        </div><article class="page single"><h1 class="single-title animated flipInX">《Dart语言基础之Map》</h1><div class="post-meta">
            <div class="post-meta-line"><span class="post-author"><a href="/" title="Author" rel=" author" class="author"><i class="fas fa-user-circle fa-fw"></i>早起的年轻人</a></span>&nbsp;<span class="post-category">收录于 <a href="/categories/flutter/"><i class="far fa-folder fa-fw"></i>Flutter</a></span></div>
            <div class="post-meta-line"><i class="far fa-calendar-alt fa-fw"></i>&nbsp;<time datetime="2021-03-29">2021-03-29</time>&nbsp;<i class="fas fa-pencil-alt fa-fw"></i>&nbsp;约 1265 字&nbsp;
                <i class="far fa-clock fa-fw"></i>&nbsp;预计阅读 3 分钟&nbsp;</div>
        </div><div class="featured-image"><img
        class="lazyload"
        src="/svg/loading.min.svg"
        data-src="https://luckly007.oss-cn-beijing.aliyuncs.com/img/90c6cc12-742e-4c9f-b318-b912f163b8d0.png"
        data-srcset="https://luckly007.oss-cn-beijing.aliyuncs.com/img/90c6cc12-742e-4c9f-b318-b912f163b8d0.png, https://luckly007.oss-cn-beijing.aliyuncs.com/img/90c6cc12-742e-4c9f-b318-b912f163b8d0.png 1.5x, https://luckly007.oss-cn-beijing.aliyuncs.com/img/90c6cc12-742e-4c9f-b318-b912f163b8d0.png 2x"
        data-sizes="auto"
        alt="https://luckly007.oss-cn-beijing.aliyuncs.com/img/90c6cc12-742e-4c9f-b318-b912f163b8d0.png"
        title="https://luckly007.oss-cn-beijing.aliyuncs.com/img/90c6cc12-742e-4c9f-b318-b912f163b8d0.png" /></div><div class="details toc" id="toc-static"  kept="">
                <div class="details-summary toc-title">
                    <span>目录</span>
                    <span><i class="details-icon fas fa-angle-right"></i></span>
                </div>
                <div class="details-content toc-content" id="toc-content-static"><nav id="TableOfContents">
  <ul>
    <li>
      <ul>
        <li><a href="#map定义">Map定义</a>
          <ul>
            <li><a href="#不指定泛型">不指定泛型</a></li>
            <li><a href="#指定泛型">指定泛型</a></li>
          </ul>
        </li>
        <li><a href="#map常见属性">Map常见属性</a></li>
        <li><a href="#map常见方法">Map常见方法</a></li>
      </ul>
    </li>
  </ul>
</nav></div>
            </div><div class="content" id="content"><blockquote>
<p>本文主要介绍</p>
</blockquote>
<h2 id="map定义">Map定义</h2>
<h3 id="不指定泛型">不指定泛型</h3>
<div class="highlight"><div class="chroma">
<table class="lntable"><tr><td class="lntd">
<pre class="chroma"><code><span class="lnt"> 1
</span><span class="lnt"> 2
</span><span class="lnt"> 3
</span><span class="lnt"> 4
</span><span class="lnt"> 5
</span><span class="lnt"> 6
</span><span class="lnt"> 7
</span><span class="lnt"> 8
</span><span class="lnt"> 9
</span><span class="lnt">10
</span><span class="lnt">11
</span><span class="lnt">12
</span><span class="lnt">13
</span><span class="lnt">14
</span><span class="lnt">15
</span><span class="lnt">16
</span><span class="lnt">17
</span></code></pre></td>
<td class="lntd">
<pre class="chroma"><code class="language-fallback" data-lang="fallback">  var map1 = {&#39;aa&#39;: &#39;aaa&#39;, &#39;bb&#39;: &#39;bbb&#39;, &#39;cc&#39;: true}; //直接赋值初始值
  print(map1); //{aa: aaa, bb: bbb, cc: true}

  Map map2 = {&#39;a&#39;: &#39;a1&#39;, &#39;b&#39;: &#39;b1&#39;}; //直接赋值初始值
  print(map2); //{a: a1, b: b1}

  map2[&#39;c&#39;] = &#39;c1&#39;;
  print(map2); //{a: a1, b: b1, c: c1}

  var map3 = new Map();
  map3[&#39;a&#39;] = &#39;a3&#39;;
  map3[2] = &#39;b3&#39;;
  print(map3); //{a: a3, 2: b3}
  print(map3[3]); //null 如果不存在，则为null
  map3[3] = null; //key存在 value存在 value的值为null
  print(map3); //{a: a3, 2: b3, 3: null}
  
</code></pre></td></tr></table>
</div>
</div><h3 id="指定泛型">指定泛型</h3>
<div class="highlight"><div class="chroma">
<table class="lntable"><tr><td class="lntd">
<pre class="chroma"><code><span class="lnt">1
</span><span class="lnt">2
</span><span class="lnt">3
</span><span class="lnt">4
</span><span class="lnt">5
</span><span class="lnt">6
</span><span class="lnt">7
</span><span class="lnt">8
</span></code></pre></td>
<td class="lntd">
<pre class="chroma"><code class="language-fallback" data-lang="fallback">var map4 = &lt;String, String&gt;{};
  Map&lt;int, String&gt; map5 = new Map(); //new也可以省略
  map5[1] = &#39;a5&#39;;
  map5[2] = &#39;b5&#39;;
  print(map5); //{1: a5, 2: b5}

  Map&lt;String, int&gt; map6 = {&#39;a6&#39;: 1, &#39;b6&#39;: 2};
  print(map6); //{a6: 1, b6: 2}
</code></pre></td></tr></table>
</div>
</div><h2 id="map常见属性">Map常见属性</h2>
<div class="highlight"><div class="chroma">
<table class="lntable"><tr><td class="lntd">
<pre class="chroma"><code><span class="lnt"> 1
</span><span class="lnt"> 2
</span><span class="lnt"> 3
</span><span class="lnt"> 4
</span><span class="lnt"> 5
</span><span class="lnt"> 6
</span><span class="lnt"> 7
</span><span class="lnt"> 8
</span><span class="lnt"> 9
</span><span class="lnt">10
</span><span class="lnt">11
</span><span class="lnt">12
</span><span class="lnt">13
</span><span class="lnt">14
</span></code></pre></td>
<td class="lntd">
<pre class="chroma"><code class="language-fallback" data-lang="fallback"> Map&lt;String, int&gt; map7 = Map();
  map7[&#39;a7&#39;] = 1;
  map7[&#39;b7&#39;] = 2;
  map7[&#39;c7&#39;] = 3;
  print(map7.length); //3 长度
  print(map7.isNotEmpty); //true  是否不为空
  print(map7.isEmpty); //flase  是否为空
  print(
      &#39;${map7.keys},${map7.keys.runtimeType}&#39;); // (a7, b7, c7),_CompactIterable&lt;String&gt;    Map的所有key
  print(
      &#39;${map7.values},${map7.values.runtimeType}&#39;); //(1, 2, 3),_CompactIterable&lt;int&gt;    Map的所有value
  print(
      &#39;${map7.entries},${map7.entries.runtimeType}&#39;); // (MapEntry(a7: 1), MapEntry(b7: 2), MapEntry(c7: 3)),MappedIterable&lt;String, MapEntry&lt;String, int&gt; Map的所有键值对

</code></pre></td></tr></table>
</div>
</div><h2 id="map常见方法">Map常见方法</h2>
<ul>
<li>新增一个键值对</li>
</ul>
<div class="highlight"><div class="chroma">
<table class="lntable"><tr><td class="lntd">
<pre class="chroma"><code><span class="lnt">1
</span><span class="lnt">2
</span><span class="lnt">3
</span><span class="lnt">4
</span></code></pre></td>
<td class="lntd">
<pre class="chroma"><code class="language-fallback" data-lang="fallback">  Map&lt;String, int&gt; map8 = Map();
  map8[&#39;a8&#39;] = 1;
  print(map8); //{a8: 1}
  
</code></pre></td></tr></table>
</div>
</div><hr>
<ul>
<li>修改一个键值对</li>
</ul>
<div class="highlight"><div class="chroma">
<table class="lntable"><tr><td class="lntd">
<pre class="chroma"><code><span class="lnt">1
</span><span class="lnt">2
</span><span class="lnt">3
</span><span class="lnt">4
</span></code></pre></td>
<td class="lntd">
<pre class="chroma"><code class="language-fallback" data-lang="fallback">  Map&lt;String, int&gt; map9 = {&#39;a9&#39;: 1, &#39;b9&#39;: 2};
  map9[&#39;a9&#39;] = 9;
  print(map9); //{a9: 9, b9: 2}  
  
</code></pre></td></tr></table>
</div>
</div><hr>
<ul>
<li>update(K key, V update(V value), {V ifAbsent()})  根据指定的Key对应的value做出修改，同时Map本身也会被修改</li>
</ul>
<div class="highlight"><div class="chroma">
<table class="lntable"><tr><td class="lntd">
<pre class="chroma"><code><span class="lnt"> 1
</span><span class="lnt"> 2
</span><span class="lnt"> 3
</span><span class="lnt"> 4
</span><span class="lnt"> 5
</span><span class="lnt"> 6
</span><span class="lnt"> 7
</span><span class="lnt"> 8
</span><span class="lnt"> 9
</span><span class="lnt">10
</span></code></pre></td>
<td class="lntd">
<pre class="chroma"><code class="language-fallback" data-lang="fallback">  Map&lt;String, int&gt; map10 = {&#39;a10&#39;: 1, &#39;b10&#39;: 2, &#39;c10&#39;: 3};
  var resultMap10 = map10.update(&#39;b10&#39;, (value) =&gt; value * 2);
  print(resultMap10); //4
  print(map10); //{a10: 1, b10: 4, c10: 3}
  
  var resultMap101 = map10.update(&#39;c&#39;, (value) =&gt; (value * 2),
      ifAbsent: () =&gt; (10)); //如果key不存在，但是有ifAbsent参数，返回idAbsent函数的值，并添加到map中
  print(&#39;$resultMap101,${resultMap101.runtimeType}&#39;); //10,int
  print(map10); //{a10: 1, b10: 4, c10: 3, c: 10}
 
</code></pre></td></tr></table>
</div>
</div><hr>
<ul>
<li>updateAll(V update(K key, V value)) 根据参数内的函数，修改map</li>
</ul>
<div class="highlight"><div class="chroma">
<table class="lntable"><tr><td class="lntd">
<pre class="chroma"><code><span class="lnt"> 1
</span><span class="lnt"> 2
</span><span class="lnt"> 3
</span><span class="lnt"> 4
</span><span class="lnt"> 5
</span><span class="lnt"> 6
</span><span class="lnt"> 7
</span><span class="lnt"> 8
</span><span class="lnt"> 9
</span><span class="lnt">10
</span><span class="lnt">11
</span><span class="lnt">12
</span><span class="lnt">13
</span><span class="lnt">14
</span><span class="lnt">15
</span><span class="lnt">16
</span></code></pre></td>
<td class="lntd">
<pre class="chroma"><code class="language-fallback" data-lang="fallback">  Map&lt;String, int&gt; map11 = {&#39;a11&#39;: 2, &#39;b11&#39;: 3, &#39;c11&#39;: 4};
  var resultMap11 = map11.updateAll((key, value) {
    return value * 2;
  });
  print(map11); //{a11: 4, b11: 6, c11: 8}
  map11.updateAll((key, value) {
    if (key == &#39;a11&#39;) {
      return value * 2;
    }
    if (key == &#39;c11&#39;) {
      return value + 1;
    }
    return 7; //如果没有这行，b11对应的value为null 这里可以看出updateall会影响所有的键值对
  });
  print(map11); //{a11: 8, b11: 7, c11: 9}
  
</code></pre></td></tr></table>
</div>
</div><hr>
<ul>
<li>remove() 删除一个key</li>
</ul>
<div class="highlight"><div class="chroma">
<table class="lntable"><tr><td class="lntd">
<pre class="chroma"><code><span class="lnt">1
</span><span class="lnt">2
</span><span class="lnt">3
</span><span class="lnt">4
</span><span class="lnt">5
</span><span class="lnt">6
</span></code></pre></td>
<td class="lntd">
<pre class="chroma"><code class="language-fallback" data-lang="fallback">  Map&lt;String, int&gt; map12 = {&#39;a12&#39;: 2, &#34;b12&#34;: 1};
  map12.remove(&#39;a12&#39;);
  print(map12); //{b12: 1}
  map12.remove(&#39;c12&#39;); //删除一个不存在的key，毫无影响，无报错无警告
  print(map12); //{b12: 1}  
  
</code></pre></td></tr></table>
</div>
</div><hr>
<ul>
<li>removeWhere(bool predicate(K key, V value))  根据函数条件批量删除key</li>
</ul>
<div class="highlight"><div class="chroma">
<table class="lntable"><tr><td class="lntd">
<pre class="chroma"><code><span class="lnt">1
</span><span class="lnt">2
</span><span class="lnt">3
</span><span class="lnt">4
</span></code></pre></td>
<td class="lntd">
<pre class="chroma"><code class="language-fallback" data-lang="fallback"> Map&lt;String, int&gt; map13 = {&#39;a13&#39;: 3, &#39;b13&#39;: 4, &#39;c13&#39;: 1};
 map13.removeWhere((key, value) =&gt; value &gt; 3);
 print(map13); //{a13: 3, c13: 1}  
 
</code></pre></td></tr></table>
</div>
</div><hr>
<ul>
<li>containsKey() 是否包含某个key  contrainsValue()是否包含某个value</li>
</ul>
<div class="highlight"><div class="chroma">
<table class="lntable"><tr><td class="lntd">
<pre class="chroma"><code><span class="lnt">1
</span><span class="lnt">2
</span><span class="lnt">3
</span></code></pre></td>
<td class="lntd">
<pre class="chroma"><code class="language-fallback" data-lang="fallback">   Map&lt;String ,int&gt; map14 = {&#39;a14&#39;:1};
   bool resultMap14 = map14.containsKey(&#39;a11&#39;); //false
   bool resultMap141 = map14.containsValue(1); //true
</code></pre></td></tr></table>
</div>
</div><hr>
<ul>
<li>forEach(void f(K key, V value)) 遍历Map ,遍历时不可add或者remove</li>
</ul>
<div class="highlight"><div class="chroma">
<table class="lntable"><tr><td class="lntd">
<pre class="chroma"><code><span class="lnt"> 1
</span><span class="lnt"> 2
</span><span class="lnt"> 3
</span><span class="lnt"> 4
</span><span class="lnt"> 5
</span><span class="lnt"> 6
</span><span class="lnt"> 7
</span><span class="lnt"> 8
</span><span class="lnt"> 9
</span><span class="lnt">10
</span><span class="lnt">11
</span><span class="lnt">12
</span><span class="lnt">13
</span><span class="lnt">14
</span><span class="lnt">15
</span><span class="lnt">16
</span><span class="lnt">17
</span></code></pre></td>
<td class="lntd">
<pre class="chroma"><code class="language-fallback" data-lang="fallback">  Map&lt;String, int&gt; map15 = {&#39;a15&#39;: 1, &#39;b15&#39;: 2, &#39;c&#39;: 3, &#39;d&#39;: 4, &#39;e&#39;: 5};
  map15.forEach((key, value) {
    print(&#39;$key,$value&#39;);
    /*
    a15,1
    b15,2
     c,3
     d,4
     e,5
    */
  });
   map15.forEach((key, value) {
    //  if(key == &#39;a15&#39;){value = 8;}
    map15[&#39;a15&#39;] = 8;
   });
   print(map15); //{a15: 8, b15: 2, c: 3, d: 4, e: 5}
   
</code></pre></td></tr></table>
</div>
</div><hr>
<ul>
<li>map() 遍历每个键值对 根据参数函数，对keyvalue做出修改，转换成其他泛型Map</li>
</ul>
<div class="highlight"><div class="chroma">
<table class="lntable"><tr><td class="lntd">
<pre class="chroma"><code><span class="lnt">1
</span><span class="lnt">2
</span><span class="lnt">3
</span><span class="lnt">4
</span><span class="lnt">5
</span><span class="lnt">6
</span></code></pre></td>
<td class="lntd">
<pre class="chroma"><code class="language-fallback" data-lang="fallback">   Map&lt;String,int&gt; map16 = {&#39;a16&#39;:7,&#34;b16&#34;:5,&#39;c16&#39;:4};
   Map&lt;int,String&gt; map17 = map16.map((key, value) {
       return MapEntry(value, key);
   });
   print(map17);
   
</code></pre></td></tr></table>
</div>
</div><hr>
<ul>
<li>addAll() 两个Map合并，类型需要一致 ，且如果key相同，则会覆盖value</li>
</ul>
<div class="highlight"><div class="chroma">
<table class="lntable"><tr><td class="lntd">
<pre class="chroma"><code><span class="lnt">1
</span><span class="lnt">2
</span><span class="lnt">3
</span><span class="lnt">4
</span><span class="lnt">5
</span></code></pre></td>
<td class="lntd">
<pre class="chroma"><code class="language-fallback" data-lang="fallback">  Map&lt;String,int&gt; map18 = {&#39;a18&#39;:1,&#39;b18&#39;:7,&#39;a19&#39;:2};
  Map&lt;String,int&gt; map19 = {&#39;a19&#39;:9};
  map18.addAll(map19);
  print(map18); //{a18: 1, b18: 7, a19: 9}   
  
</code></pre></td></tr></table>
</div>
</div><hr>
<ul>
<li>addEntres(key,value)  两个Map合并，类型需要一致 ，且如果key相同，则会覆盖value</li>
</ul>
<div class="highlight"><div class="chroma">
<table class="lntable"><tr><td class="lntd">
<pre class="chroma"><code><span class="lnt">1
</span><span class="lnt">2
</span><span class="lnt">3
</span><span class="lnt">4
</span><span class="lnt">5
</span></code></pre></td>
<td class="lntd">
<pre class="chroma"><code class="language-fallback" data-lang="fallback">  Map&lt;String,int&gt; map20 = {&#39;a20&#39;:2,&#39;b20&#39;:3};
  Map&lt;String,int&gt; map21 = {&#39;a21&#39;:5,&#39;b21&#39;:9};
  map20.addEntries(map21.entries);
  print(map20); //{a20: 2, b20: 3, a21: 5, b21: 9}  
  
</code></pre></td></tr></table>
</div>
</div><ul>
<li>putIfAbsent()  存在key则返回value，查不到则返回值 不修改Map</li>
</ul>
<div class="highlight"><div class="chroma">
<table class="lntable"><tr><td class="lntd">
<pre class="chroma"><code><span class="lnt">1
</span><span class="lnt">2
</span><span class="lnt">3
</span><span class="lnt">4
</span><span class="lnt">5
</span><span class="lnt">6
</span><span class="lnt">7
</span></code></pre></td>
<td class="lntd">
<pre class="chroma"><code class="language-fallback" data-lang="fallback"> Map&lt;String,int&gt; map22 = {&#39;a22&#39;:3,&#39;b22&#39;:4};
 var resultMap22 = map22.putIfAbsent(&#39;a22&#39;, () =&gt; 2); //存在key则返回value，查不到则返回 2 不修改Map
 print(&#39;$resultMap22,$map22&#39;);//3,{a22: 3, b22: 4} 
 
 var resultMap221 = map2.putIfAbsent(&#39;a2&#39;, () =&gt; 1);
 print(&#39;$resultMap221,$map22&#39;); //1,{a22: 3, b22: 4} //存在key则返回value，查不到则返回 1 不修改Map 
 
</code></pre></td></tr></table>
</div>
</div><hr>
<ul>
<li>cast() 泛型提升为其父祖类</li>
</ul>
<div class="highlight"><div class="chroma">
<table class="lntable"><tr><td class="lntd">
<pre class="chroma"><code><span class="lnt">1
</span><span class="lnt">2
</span><span class="lnt">3
</span><span class="lnt">4
</span></code></pre></td>
<td class="lntd">
<pre class="chroma"><code class="language-fallback" data-lang="fallback"> Map&lt;String,int&gt; map23 = {&#39;a33&#39;:3};
 Map&lt;Object,Object&gt; map24 = map23.cast();
 print(&#39;$map23,${map23.runtimeType},$map24,${map24.runtimeType}&#39;); //{a33: 3},_InternalLinkedHashMap&lt;String, int&gt;,{a33: 3},CastMap&lt;String, int, Object, Object&gt;

</code></pre></td></tr></table>
</div>
</div><hr>
<ul>
<li>清除所有键值对，对所有类型map都适用</li>
</ul>
<div class="highlight"><div class="chroma">
<table class="lntable"><tr><td class="lntd">
<pre class="chroma"><code><span class="lnt">1
</span><span class="lnt">2
</span><span class="lnt">3
</span></code></pre></td>
<td class="lntd">
<pre class="chroma"><code class="language-fallback" data-lang="fallback"> Map&lt;String,int&gt; map25 = {&#39;a25&#39;:2,&#39;b25&#39;:3};
 map25.clear();
 print(&#39;$map25,${map25.runtimeType}&#39;); //{},_InternalLinkedHashMap&lt;String, int&gt;
</code></pre></td></tr></table>
</div>
</div></div><div class="post-footer" id="post-footer">
    <div class="post-info">
        <div class="post-info-line">
            <div class="post-info-mod">
                <span>更新于 2021-03-29</span>
            </div>
            <div class="post-info-license"></div>
        </div>
        <div class="post-info-line">
            <div class="post-info-md"></div>
            <div class="post-info-share">
                <span><a href="javascript:void(0);" title="分享到 Twitter" data-sharer="twitter" data-url="https://luckly.work/dart%E8%AF%AD%E8%A8%80%E5%9F%BA%E7%A1%80%E4%B9%8Bmap/" data-title="《Dart语言基础之Map》" data-hashtags="flutter"><i class="fab fa-twitter fa-fw"></i></a><a href="javascript:void(0);" title="分享到 Facebook" data-sharer="facebook" data-url="https://luckly.work/dart%E8%AF%AD%E8%A8%80%E5%9F%BA%E7%A1%80%E4%B9%8Bmap/" data-hashtag="flutter"><i class="fab fa-facebook-square fa-fw"></i></a><a href="javascript:void(0);" title="分享到 Hacker News" data-sharer="hackernews" data-url="https://luckly.work/dart%E8%AF%AD%E8%A8%80%E5%9F%BA%E7%A1%80%E4%B9%8Bmap/" data-title="《Dart语言基础之Map》"><i class="fab fa-hacker-news fa-fw"></i></a><a href="javascript:void(0);" title="分享到 Line" data-sharer="line" data-url="https://luckly.work/dart%E8%AF%AD%E8%A8%80%E5%9F%BA%E7%A1%80%E4%B9%8Bmap/" data-title="《Dart语言基础之Map》"><i data-svg-src="https://cdn.jsdelivr.net/npm/simple-icons@2.14.0/icons/line.svg"></i></a><a href="javascript:void(0);" title="分享到 微博" data-sharer="weibo" data-url="https://luckly.work/dart%E8%AF%AD%E8%A8%80%E5%9F%BA%E7%A1%80%E4%B9%8Bmap/" data-title="《Dart语言基础之Map》" data-image="https://luckly007.oss-cn-beijing.aliyuncs.com/img/90c6cc12-742e-4c9f-b318-b912f163b8d0.png"><i class="fab fa-weibo fa-fw"></i></a></span>
            </div>
        </div>
    </div>

    <div class="post-info-more">
        <section class="post-tags"><i class="fas fa-tags fa-fw"></i>&nbsp;<a href="/tags/flutter/">flutter</a></section>
        <section>
            <span><a href="javascript:void(0);" onclick="window.history.back();">返回</a></span>&nbsp;|&nbsp;<span><a href="/">主页</a></span>
        </section>
    </div>

    <div class="post-nav"><a href="/dart%E8%AF%AD%E8%A8%80%E5%9F%BA%E7%A1%80%E4%B9%8Bdata/" class="prev" rel="prev" title="《Dart语言基础之Data》"><i class="fas fa-angle-left fa-fw"></i>《Dart语言基础之Data》</a>
            <a href="/%E5%9C%A8flutter%E4%B8%AD%E5%88%B6%E4%BD%9C%E5%A4%96%E5%8D%96%E5%BA%94%E7%94%A8/" class="next" rel="next" title="《在Flutter中制作外卖应用》">《在Flutter中制作外卖应用》<i class="fas fa-angle-right fa-fw"></i></a></div>
</div>
</article></div>
            </main><footer class="footer">
        <div class="footer-container"><div class="footer-line">由 <a href="https://gohugo.io/" target="_blank" rel="noopener noreffer" title="Hugo 0.79.1">Hugo</a> 强力驱动 | 主题 - <a href="https://github.com/dillonzq/LoveIt" target="_blank" rel="noopener noreffer" title="LoveIt 0.2.10"><i class="far fa-kiss-wink-heart fa-fw"></i> LoveIt</a>
                </div><div class="footer-line"><i class="far fa-copyright fa-fw"></i><span itemprop="copyrightYear">2021</span><span class="author" itemprop="copyrightHolder">&nbsp;<a href="/" target="_blank">早起的年轻人</a></span>&nbsp;|&nbsp;<span class="license"><a rel="license external nofollow noopener noreffer" href="https://creativecommons.org/licenses/by-nc/4.0/" target="_blank">CC BY-NC 4.0</a></span></div>
        </div>
    </footer></div>

        <div id="fixed-buttons"><a href="#" id="back-to-top" class="fixed-button" title="回到顶部">
                <i class="fas fa-arrow-up fa-fw"></i>
            </a><a href="#" id="view-comments" class="fixed-button" title="查看评论">
                <i class="fas fa-comment fa-fw"></i>
            </a>
        </div><link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/cookieconsent@3.1.1/build/cookieconsent.min.css"><script type="text/javascript" src="https://cdn.jsdelivr.net/npm/smooth-scroll@16.1.3/dist/smooth-scroll.min.js"></script><script type="text/javascript" src="https://cdn.jsdelivr.net/npm/autocomplete.js@0.37.1/dist/autocomplete.min.js"></script><script type="text/javascript" src="https://cdn.jsdelivr.net/npm/algoliasearch@4.2.0/dist/algoliasearch-lite.umd.min.js"></script><script type="text/javascript" src="https://cdn.jsdelivr.net/npm/lazysizes@5.2.2/lazysizes.min.js"></script><script type="text/javascript" src="https://cdn.jsdelivr.net/npm/clipboard@2.0.6/dist/clipboard.min.js"></script><script type="text/javascript" src="https://cdn.jsdelivr.net/npm/sharer.js@0.4.0/sharer.min.js"></script><script type="text/javascript" src="https://cdn.jsdelivr.net/npm/cookieconsent@3.1.1/build/cookieconsent.min.js"></script><script type="text/javascript">window.config={"code":{"copyTitle":"复制到剪贴板","maxShownLines":10},"comment":{},"cookieconsent":{"content":{"dismiss":"同意","link":"了解更多","message":"本网站使用 Cookies 来改善您的浏览体验."},"enable":true,"palette":{"button":{"background":"#f0f0f0"},"popup":{"background":"#1aa3ff"}},"theme":"edgeless"},"search":{"algoliaAppID":"PASDMWALPK","algoliaIndex":"index.zh-cn","algoliaSearchKey":"b42948e51daaa93df92381c8e2ac0f93","highlightTag":"em","maxResultLength":10,"noResultsFound":"没有找到结果","snippetLength":50,"type":"algolia"}};</script><script type="text/javascript" src="/js/theme.min.js"></script></body>
</html>
